﻿using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;

namespace HelpCatch
{
    public class WTHelp
    {
        private FrameworkElement mainControl;
        private Point mousePosition;

        /// <summary>
        /// Initialize WTHelp, hook up the mousemove handler to get the mouse position
        /// </summary>
        /// <param name="main">The root element</param>
        public WTHelp(FrameworkElement main)
        {
            this.mainControl = main;
            main.MouseMove += changeMousePosition;
        }

        /// <summary>
        /// Update the position of the mouse
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void changeMousePosition(object sender, MouseEventArgs e)
        {
            mousePosition = e.GetPosition(mainControl);
        }


        #region HelpKey Property

        /// <summary>
        /// Attached property for WTHelp to set the Key
        /// </summary>
        public static readonly DependencyProperty HelpKeyProperty = DependencyProperty.RegisterAttached(
              "Key",
              typeof(string),
              typeof(WTHelp),
              null);

        public static void SetKey(DependencyObject obj, string propertyValue)
        {
            obj.SetValue(HelpKeyProperty, propertyValue);
        }

        public static string GetKey(DependencyObject obj)
        {
            return (string)obj.GetValue(HelpKeyProperty);


        }
        #endregion

        /// <summary>
        /// Initialize the WThelp control with the root element
        /// </summary>
        /// <param name="main">The root element of the application</param>
        public static void Initialize(FrameworkElement main)
        {
            WTHelp trigger = new WTHelp(main);
            HtmlPage.RegisterScriptableObject("JS", trigger);
        }

        /// <summary>
        /// Scriptable Help method that can be triggered by javascript,
        /// It checks the visual tree, and if the selected item has help, it shows it.
        /// </summary>
        [ScriptableMember()]
        public void Help()
        {
            //Get the visual stack of the current position
            var visualStackOnLocation = VisualTreeHelper.FindElementsInHostCoordinates(mousePosition, mainControl);
            
            //Are there items on this position
            if (visualStackOnLocation.ToList().Count > 0)
            {
                //Go trough all the levels, from the topmost to the lowest
                for (int level = 0; level < visualStackOnLocation.ToList().Count; level++)
                {
                    //Get an element
                    FrameworkElement currentElement = (FrameworkElement)visualStackOnLocation.ToList()[level];
                    //Does this element hava a helpkey associated
                    var key = currentElement.GetValue(HelpKeyProperty);
                    if (key != null)
                    {
                        //Show the associated help
                        //TODO: Show real help here
                        MessageBox.Show(HelpRepository.GetHelp(key.ToString()));
                        //When help was found we should not continue searching, so exit the method.
                        return;
                    }
                }


            }
        }


    }
}
